package business.client.managers;

import client.entities.ClientVO;
import client.exceptions.BadClientException;
import client.exceptions.ClientNotHereException;
import client.exceptions.IncompleteClientException;
import client.exceptions.MissingClientException;
import dbExceptions.DataBaseErrorException;
import persistence.client.clientDAO.ClientDAO;
import business.client.entities.Client;
import business.client.interfaces.ClientMgt;

public class ClientMgr implements ClientMgt {

	private static ClientMgr instance = null;

	private ClientMgr() {
	}

	public static ClientMgr getInstnace() {
		if (instance == null)
			instance = new ClientMgr();
		return instance;
	}

	
	public void addClient(ClientVO client) throws IncompleteClientException,
			MissingClientException, DataBaseErrorException, BadClientException {
		if (client != null && client.getName() != null
				&& client.getLastName() != null && client.getCel() != null) {
			ClientDAO aux = ClientDAO.getInstance();
			aux.addClient(new Client(client));
		} else {
			throw new IncompleteClientException("Faltan datos al Cliente");
		}
	}

	
	public void updateClient(ClientVO client) throws IncompleteClientException,
			MissingClientException, BadClientException, DataBaseErrorException {
		if (client != null && client.getName() != null
				&& client.getLastName() != null && client.getCel() != null) {
			ClientDAO aux = ClientDAO.getInstance();
			aux.updateClient(new Client(client));
		} else {
			throw new IncompleteClientException("Faltan datos al Cliente");
		}
	}

	public ClientVO[] getClients() {
		return this.toVOs(ClientDAO.getInstance().getClients());
	}

	
	public void deleteClient(ClientVO cli) throws MissingClientException,
			DataBaseErrorException, BadClientException {
		ClientDAO.getInstance().deleteClient(new Client(cli));
	}

	public ClientVO getClient(int ci) throws ClientNotHereException {		
		Client aux = ClientDAO.getInstance().getClient(ci);
		return (aux!=null) ? aux.toVO() : null;
	}
	
	
	// Convertidor de ARRAYS
	private ClientVO[] toVOs(Client[] cli){
		ClientVO[] temp = (cli!=null) ? new ClientVO[cli.length] : null;
		int i = 0;
		for(Client e : cli){
			temp[i] = e.toVO();
			i++;
		}return temp;
	}

}
